package com.LeetCode.List;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 重排链表
 */
public class ReSortList {

    public void reorderList(ListNode head) {
        List<ListNode> listNodes = new ArrayList<>();
        ListNode pNode = head;
        while(pNode!=null){
            listNodes.add(pNode);
            pNode = pNode.next;
        }
        int l = 0, r = listNodes.size()-1;
        while(l<r){
            ListNode leftNode = listNodes.get(l);
            ListNode rightNode = listNodes.get(r);
            ListNode next = leftNode.next;
            leftNode.next = rightNode;
            rightNode.next = next;
            l++;
            r--;
        }
        listNodes.get(l).next = null;
    }

    @Test
    public void test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        reorderList(node1);
        ListNode.printList(node1);
    }
}
